package com.yuyang.wifi.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yuyang.wifi.R;
import com.yuyang.wifi.dialog.NewLoadingDialog;
import com.yuyang.wifi.utils.EmptyUtils;
import com.yuyang.wifi.utils.eventbus.BaseEvent;
import com.yuyang.wifi.utils.eventbus.BaseEventManager;
import com.yuyang.wifi.utils.eventbus.BaseEventObserver;
import com.yuyang.wifi.utils.network.HttpErrorHandlerImp;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by Administrator on 2016/3/23.
 */
public abstract class BaseFragment extends com.trello.rxlifecycle.components.support.RxFragment
        implements View.OnClickListener, BaseEventObserver {

    protected List<SuperSubject> superSubjectList = new ArrayList<>();
    protected Activity activity;
    protected HttpErrorHandlerImp httpErrorHandlerImp;
    protected DialogHandlerImp dialogHandlerImp;
    protected Unbinder mUnbinder;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        try {
//            //用户手动修改权限，app进程杀掉,监测到以后，自动重启app
//            if (savedInstanceState != null) {
//                Intent intent = new Intent(activity, SplashActivity.class);
//                //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                startActivity(intent);
//                ActivityManager.getInstance().finishAllActivity();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println("error : " + e.getLocalizedMessage());
//        }
        httpErrorHandlerImp = new HttpErrorHandlerImp(activity);
        dialogHandlerImp = new DialogHandlerImp(activity);
        //注册EventBus
        BaseEventManager.register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View root = inflater.inflate(getLayoutResId(), container, false);
        //绑定控件
        mUnbinder = ButterKnife.bind(this, root);
        return root;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initActionBar();
        //初始化View和数据
        initBizView();
    }

    protected abstract void initActionBar();

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
//        if (!hidden && mImmersionBar != null)
//            mImmersionBar.init();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isHidden()) {
            onFragmentVisibleResume();
        }
    }


    public void onFragmentVisibleResume() {

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        unRegisterObserver();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //解绑
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
        //销毁EventBus
        BaseEventManager.unregister(this);
    }

    protected void initBusObserver() {

    }

    protected void initButtonObserver() {

    }


    protected void initBar() {

    }


    protected <T> SuperSubject<T> registerObserver(String tag, Class<T> clazz, final SuperSubject.onSubscribe<T> tCall) {
        SuperSubject<T> superSubject = RxBus.getInstance().register(tag, clazz);
        superSubject.observeOnMainThread(tCall);
        superSubjectList.add(superSubject);
        return superSubject;
    }

    protected <T> SuperSubject<T> registerObserver(String tag, Class<T> clazz) {
        SuperSubject<T> superSubject = RxBus.getInstance().register(tag, clazz);
        superSubjectList.add(superSubject);
        return superSubject;
    }


    protected void unRegisterObserver() {
        if (!EmptyUtils.isEmpty(superSubjectList)) {
            for (SuperSubject superSubject : superSubjectList) {
                RxBus.getInstance().unRegister(superSubject);
            }
        }
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    protected synchronized void showDialog() {
        dialogHandlerImp.showDialog();
    }

    protected synchronized void dismissDialog() {
        dialogHandlerImp.dismissDialog();
    }


    protected void initPtrRefreshLayout(Context context, PtrFrameLayout frameLayout, PtrHandler handler) {
        PtrMyTextHeader header = new PtrMyTextHeader(context);
        frameLayout.setHeaderView(header);
        frameLayout.addPtrUIHandler(header);
        frameLayout.disableWhenHorizontalMove(true);
        frameLayout.setPtrHandler(handler);
        frameLayout.setRatioOfHeaderHeightToRefresh(0.2f);
    }

    protected abstract int getLayoutResId();

    //加载视图
    protected abstract void initBizView();


    @Override
    public void onClick(View v) {

    }

    //没有更多了
    protected View showFooterNoMoreData() {
        View footerView = LayoutInflater.from(getActivity()).inflate(R.layout.item_footer_no_more, null);
        footerView.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        return footerView;
    }


    @Subscribe
    @Override
    public void onMessageEventMain(BaseEvent baseEvent) {

    }

    @Subscribe
    @Override
    public void onMessageEventPost(BaseEvent baseEvent) {

    }

    @Subscribe
    @Override
    public void onMessageEventBackground(BaseEvent baseEvent) {

    }

    @Subscribe
    @Override
    public void onMessageEventAsync(BaseEvent baseEvent) {

    }

    // loading
    protected NewLoadingDialog newLoadingDialog;

    /**
     * 显示正在加载。。。
     */
    public void showLoadingDialog() {

        if (null != newLoadingDialog) {
            newLoadingDialog.show();
        } else {
            newLoadingDialog = new NewLoadingDialog(activity);
            newLoadingDialog.setCanceledOnTouchOutside(false);
            newLoadingDialog.show();
        }
    }

    /**
     * 取消加载
     */
    public void dismissLoadingDialog() {
        if (null != newLoadingDialog) {
            newLoadingDialog.dismiss();
        }
    }

    public void showUploadDownLoadingDialog() {

        if (null != newLoadingDialog) {
            newLoadingDialog.show();
        } else {
            newLoadingDialog = new NewLoadingDialog(activity);
            newLoadingDialog.setCanceledOnTouchOutside(false);
            newLoadingDialog.show();
        }
    }


    /**
     * 取消加载
     */
    public void dismissUploadDownLoadingDialog() {
        if (null != newLoadingDialog) {
            newLoadingDialog.dismiss();
        }
    }

}
